<?php
/**
 * Created by PhpStorm.
 * User: Raytine
 * Date: 2019/5/14
 * Time: 23:21
 */

namespace App\Admin\Extensions;

use App\Admin\ExportCollection\ExcelCollection;
use App\model\Industry;
use App\model\Project;
use App\User;
use Carbon\Carbon;
use Encore\Admin\Grid\Exporters\AbstractExporter;

use Maatwebsite\Excel\Facades\Excel;


class CustomerExporter extends AbstractExporter
{


    public function export()
    {
        ini_set('memory_limit', '1450M');

        $rows1=collect([[
            '客户id',
            '客户名称',
            '联系方式',
            '业务员',
            '推广人',
            '企业名称',
            '状态',
            '跟进状态',
            '行业名称',
            '项目名称',
            '渠道来源',
            '需求面积',
            '需求类型',
            '等级名称',
            '省份',
            '城市',
            '跟进记录',
            '创建时间'
            ]]);

        $rows = collect($this->getData())->map(function ($item) {
            $follow='';
             if (empty($item['follow'])){
                $follow='暂无跟进记录';
             }else{
                 foreach ($item['follow'] as $value){
                     $follow.='描述:'.$value['desc'].'  时间:'.$value['created_at']."\n";
                 }
             }
            return [
                $item['customer_id'],
                $item['name'],
                $item['mobile'],
                User::find($item['user_id'])->name ?? '',
                User::find($item['writer_id'])->name ?? '',
                $item['company_name'],
                $item['statusDesc'],
                $item['follow_status_desc'],
                $item['industry']['name'] ?? '',
                $item['project']['name'] ?? '',
                $item['source']['name'] ?? '',
                $item['area']['name'] ?? '',
                $item['demand']['name'] ?? '',
                $item['level']['name'] ?? '',
                $item['city1'],
                $item['city2'],
                $follow,
                $item['created_at']

            ];
        });
        $rows=$rows1->merge($rows);

        return Excel::download(new ExcelCollection($rows), '客户列表'.Carbon::today()->toDateString().'.xlsx')->send();

    }
}
